<?php
/**
* VIDEO CONFERENCES
* Copyright (c) 2011 
* Author: Dmitriy Tovkach (ragvalod@gmail.com)
* @license		GNU/GPL
**/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.controller');

require_once( JPATH_ROOT.DS.'components'.DS.'com_community'.DS.'libraries'.DS.'core.php');

require_once (JPATH_COMPONENT.DS.'community.php');
//require_once( JPATH_ROOT.DS.'components'.DS.'com_community'.DS.'controllers'.DS.'videoconferences.php');

class VideoconferencesController extends JController
{
	/**

	 */
	 
	 
	function fix_video_conferences($id)
	{
		$model = CFactory::getModel( 'Events' );
		$db = & $model->getDBO();
		$query	= "UPDATE ". $db->nameQuote('#__community_events')
				. ' SET ' . $db->nameQuote('created').'=' . $db->Quote(JFactory::getDate()->toMySQL() )
				. ' WHERE ' . $db->nameQuote('type').'=' . $db->Quote( 'videoconference' )
				. ' AND ' . $db->nameQuote( 'contentid' ) .'=' . $db->Quote( '1' )
				. ' AND ' . $db->nameQuote( 'id' ) .'=' . $db->Quote( $id );

		$db->setQuery( $query );
		$db->query( $query );


/*		$query	= "DELETE FROM ". $db->nameQuote('#__community_events')
				. ' WHERE ' . $db->nameQuote('type').'=' . $db->Quote( 'videoconference' )
				. ' AND ' . $db->nameQuote( 'created' ) .'<= DATE_SUB(' . $db->Quote(JFactory::getDate()->toMySQL() ) .' ,INTERVAL 1 HOUR)';

		$db->setQuery( $query );
		$db->query( $query );
*/
/*		
		echo "<pre>";
		print_r($active_video_conferences);
		echo "</pre>";
*/
	}
	 
	function display()
        {
           $user =& JFactory::getUser();
	   
	   if ($user->guest == 1)
	    {
		$msg = JText::_( 'Error login');
		$link = JRoute::_('index.php?option=com_community', false);
		$this->setRedirect($link, $msg);
	    }
	   $id_converence = JRequest::getVar('id',0,'','GET');
	   $social  = new VideoSocial();
	   $videoconference = $social->GetVideoconference($id_converence);
	   
	   JRequest::setVar('conference_id',$id_converence,'POST');
	   JRequest::setVar('view','videoconference'); // force it to be the polls view
           parent::display(0); 
        }
        function endvideoconferences()
        {
           $id_converence = JRequest::getVar('id',0,'','GET');
	   $social  = new VideoSocial();
	   $result = $social->EndVideoconference($id_converence);
	   if ($result == 0)
	   {
		$msg = JText::_( 'Videoconference deleted');
		$link = JRoute::_('index.php?option=com_community&view=videoconferences', false);
		$this->setRedirect($link, $msg);
	   }
        }
	function addtovideoconferences()
        {
	        $model = CFactory::getModel( 'Events' );
		$db = & $model->getDBO();
		
		$event			=& JTable::getInstance( 'Event' , 'CTable' );
		$event->load( $cid );
		
	
		$query	= 'SELECT memberid FROM ' . $db->nameQuote('#__community_events_members') . ' '
				. 'WHERE ' . $db->nameQuote('eventid').'=' . $db->Quote( $cid ) . ' '
				. 'AND ' . $db->nameQuote( 'status' ) . ' IN ('.COMMUNITY_EVENT_STATUS_INVITED.','.COMMUNITY_EVENT_STATUS_ATTEND.','.COMMUNITY_EVENT_STATUS_MAYBE.')';

		$db->setQuery( $query );
		$members = $db->loadAssocList();

		if($db->getErrorNum())
		{
			JError::raiseError( 500, $db->stderr());
		}
		
		$conf_url = JRoute::_('index.php?option=com_videoconference&id='.$cid.'&SessionId='.$tokbox_id,false);
		$title = JText::_('COM_COMMUNITY_VIDEOCONFERENCE_STARTED_NOTIFICATION');
		
		for($i = 0; $i < count($members); $i++)
		{
			$query	= 'SELECT name FROM ' . $db->nameQuote('#__users') . ' '
				. 'WHERE ' . $db->nameQuote('id').'=' . $db->Quote( $members[$i]['memberid'] );

			$db->setQuery( $query );
			$user_name = $db->loadResult();

			if($db->getErrorNum())
			{
				JError::raiseError( 500, $db->stderr());
			}
			//$message = "Dear $user_name, <br>Please let us inform you that Video Conferene \"{$event->title}\" just  started. <br>Please click <a href=\"$conf_url\">here</a> to join to the conference.";
			$message = JText::sprintf('COM_COMMUNITY_VIDEOCONFERENCE_STARTED_MESSAGE' , $user_name, $event->title, $conf_url );
		
			$query	= 'INSERT INTO '
					. $db->nameQuote( '#__community_custom_notifications' )
					. ' (eventid,userid,message,title) '
					. 'VALUES ('
					. "$cid,"
					. $members[$i]['memberid'].','
					. "'$message',"
					. "'$title'"
					. ')';

			$db->setQuery($query);
			$db->loadResult();

			if ($db->getErrorNum())
			{
				JError::raiseError(500, $db->stderr());
			}
		}
			
	}

}        
?>